package cn.bingo.yunsj.scan;

import cn.bingo.yunsj.domain.RpModel;
import cn.bingo.yunsj.domain.RpNotSend;
import cn.bingo.yunsj.domain.RpRule;
import cn.bingo.yunsj.domain.RpUser;
import cn.bingo.yunsj.mail.model.MailModel;
import cn.bingo.yunsj.mapper.RpModelMapper;
import cn.bingo.yunsj.mapper.RpNotSendMapper;
import cn.bingo.yunsj.mapper.RpRuleMapper;
import cn.bingo.yunsj.mapper.RpUserMapper;
import cn.bingo.yunsj.service.MailService;
import cn.bingo.yunsj.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据库扫描
 */
@Component
public class ScanMysql {

    @Autowired
    private RpRuleMapper rpRuleMapper;

    @Autowired
    private RpModelMapper rpModelMapper;

    @Autowired
    private RpUserMapper rpUserMapper;

    @Autowired
    private RpNotSendMapper rpNotSendMapper;

    @Autowired
    private MailService mailService;

    private String all = "zhoumingxin@bingosoft.net,sujiacheng@bingosoft.net";

    private static final Logger logger= LoggerFactory.getLogger(ScanMysql.class);

    /**
     * 这里需要自动扫描数据库
     */
    @Transactional
    public void sacnMysql(){
        // 将所有的用户查出来
        List<RpUser> rpUsers = rpUserMapper.selectAll();
        rpUsers.forEach(e->{
            // 表示已经发送了
            if(!StringUtils.equals("0",e.getStandby())){
                // to do 这里还需要处理新员工入职情形
                logger.info("该用户已经成功发送！");
            }else {
                long between_days = DateUtils.between_days(e.getJoinDate());
                // 新员工入职贺卡
                if (between_days == 0) {
                    //开始发送邮件
                    RpRule rpRule = rpRuleMapper.selectByName("新入职贺卡规则");
                    List<RpModel> rpModels = rpModelMapper.searchByRule(rpRule.getRuleName());
                    if (rpModels.size() == 0) {
                        logger.info("新入职贺卡模板没有设置");
                    } else {
                        MailModel mailModel = new MailModel();
                        mailModel.setName(rpModels.get(0).getModelName());  //先选定第一个模板
                        if (StringUtils.equals(rpRule.getRuleCc(), "全体员工")) {
                            rpRule.setRuleCc(all);
                        }else if (StringUtils.equals(rpRule.getRuleCc(), "无")) {

                        }
                        mailModel.setCcEmail(rpRule.getRuleCc());    //设定好的抄送对象
                        mailModel.setEmail(e.getExEmail());         //当前扫描到的用户邮箱
                        mailModel.setWay(rpRule.getWay());      // 设定方法方式
                        mailService.send(mailModel);        //开始发送
                        // 更新用户字段
                        rpUserMapper.updateStandby(e.getStandby()+1,e.getUserId());
                    }
                }
                if (between_days > 365) {
                    logger.info("开始计算年限贺卡");
                    RpRule rpRule = rpRuleMapper.selectByName("年限贺卡规则");
                    RpNotSend rpNotSend = new RpNotSend();
                    List<RpModel> rpModels = rpModelMapper.searchByRule(rpRule.getRuleName());
                    if (rpModels.size() == 0) {
                        logger.info("年限贺卡规则没有设置模板");
                    } else {
                        // 在记录到未发送之前先判断未发送里面有没有
                        if (rpNotSendMapper.selectByPrimaryKey(e.getUserId()) == null) {
                            logger.info("开始记录未发送");
                            // 记录到未发送里面去
                            rpNotSend.setUserModel(rpModels.get(0).getModelName());    //用户模板
                            if (StringUtils.equals("全体员工", rpRule.getRuleCc())) {
                                rpNotSend.setRecipient(all);                // 抄送人
                            }else if (StringUtils.equals(rpRule.getRuleCc(), "无")) {

                            }
                            rpNotSend.setSender(rpRule.getRuleSender());    //接收人
                            rpNotSend.setUpdatedBy(rpRule.getUpdateBy());
                            rpNotSend.setUpdatedTime(rpRule.getUpdateTime());
                            rpNotSend.setWay(rpRule.getWay());
                            rpNotSend.setNotSendId(e.getUserId());
                            rpNotSendMapper.insert(rpNotSend);
                        } else {
                            // 年限贺卡
                            if (between_days >= 0 && between_days <= 6) {
                                //开始发送贺卡
                                MailModel mailModel = new MailModel();
                                mailModel.setName(rpModels.get(0).getModelName());  //先选定第一个模板
                                if (StringUtils.equals(rpRule.getRuleCc(), "全体员工")) {
                                    rpRule.setRuleCc(all);
                                } else if (StringUtils.equals(rpRule.getRuleCc(), "无")) {

                                }
                                mailModel.setCcEmail(rpRule.getRuleCc());    //设定好的抄送对象
                                mailModel.setEmail(e.getExEmail());         //当前扫描到的用户邮箱
                                mailModel.setWay(rpRule.getWay());      // 设定方法方式
                                mailService.send(mailModel);        //开始发送
                                rpUserMapper.updateStandby(e.getStandby()+1,e.getUserId());
                            }
                        }
                    }
                }
            }
        });
    }
}
